package cn.stylefeng.guns.modular.recy.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.stylefeng.guns.modular.weixin.utils.Utils;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.*;
import java.io.*;
import org.springframework.ui.*;
import javax.servlet.http.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.baomidou.mybatisplus.core.toolkit.support.*;
import org.springframework.util.*;
import cn.hutool.core.util.*;
import cn.hutool.core.date.*;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.*;
import java.util.*;
import java.net.*;

public class ControllerUtil
{
    public static void relativeEntity(final IService service, final Serializable id, final Model model) {
        if (ObjectUtil.isNotNull(id)) {
            final Object object = service.getById(id);
            model.addAttribute(StringUtils.firstCharToLower(object.getClass().getSimpleName()), object);
        }
    }
    
    public static <T> QueryWrapper<T> createQuery(final Serializable model, final HttpServletRequest request) {
        return createQuery(model, request, false);
    }
    
    public static <T> QueryWrapper<T> createQuery(final Serializable model, final HttpServletRequest request, final boolean nullable) {
        final QueryWrapper<T> queryWrapper = (QueryWrapper<T>)new QueryWrapper();
        final Field[] fields = ReflectUtil.getFields((Class)model.getClass());
        final Map<String, ColumnCache> columnMap = LambdaUtils.getColumnMap(model.getClass().getName());
        for (final Field field : fields) {
            final String name = field.getName();
            final ColumnCache columnCache = columnMap.get(name.toUpperCase());
            if (columnCache != null) {
                final String column = columnCache.getColumn();
                final Object o = ReflectUtil.getFieldValue(model, field);
                if (o != null) {
                    if (o instanceof String) {
                        queryWrapper.like(!ObjectUtils.isEmpty(o), column, o);
                    }
                    else if (o instanceof Date) {
                        queryWrapper.gt(!ObjectUtils.isEmpty(o), column, o);
                        final String endTime = request.getParameter(StrUtil.toCamelCase((CharSequence)column) + "End");
                        queryWrapper.lt(org.apache.commons.lang3.StringUtils.isNotBlank((CharSequence)endTime), column, DateUtil.parse(endTime));
                    }
                    else {
                        queryWrapper.eq(!ObjectUtils.isEmpty(o), column, o);
                    }
                }
                else if (nullable) {
                    queryWrapper.isNull(column);
                }
            }
        }
        return queryWrapper;
    }
    
    public static String getRealPath(final String path) {
        /*final URL resource = ControllerUtil.class.getResource(path);
        System.out.println("ControllerUtil.class路径："+resource.toString());
        if (ObjectUtil.isNotNull(resource)) {
            String path1 = resource.getPath();
            System.out.println("path1:"+path1);
            String rawPath = new File(path1).getAbsolutePath();
            if(StrUtil.containsIgnoreCase(rawPath,".jar")){
                rawPath = Utils.getJarRoot()+"/"+path;
            }
                System.out.println("rawPath路径："+rawPath);
                return rawPath;
        }*/
        return Utils.getJarRoot()+"/"+path;

    }
    public static String saveFile(String uploadDir,InputStream inputStream,String name){
        String saveFileName = new DateTime().toString("yyyyMMddHHmmsss");//UUID.randomUUID().toString();
        return saveFile(uploadDir,inputStream,name,saveFileName);
    }
    public static String saveFile(String uploadDir,InputStream inputStream,String name,String saveFileName){
        String classRealPath = getRealPath(uploadDir);
        //System.out.println("classRealPath路径："+classRealPath);
        if(StrUtil.isBlank(classRealPath)){
            String realPath = getRealPath("/");
            File tarDir = new File(realPath,uploadDir);
            if(!tarDir.exists()){
                tarDir.mkdirs();
            }
            classRealPath = tarDir.getPath();
        }

        System.out.println("classRealPath路径："+classRealPath);
        String fileName = saveFileName + "." + FileUtil.extName(name);
        File fileSave = new File(classRealPath,fileName);
        byte[] fileData = IoUtil.readBytes(inputStream);
        FileUtil.writeBytes(fileData,fileSave);
        /*boolean isJar = Utils.isRunInJar();
        if(!isJar) {
            String webRootPath = ToolUtil.getWebRootPath(uploadDir);
            System.out.println("ToolUtil路径：" + webRootPath);
            if (StrUtil.isBlank(webRootPath)) {
                String webRootPath1 = ToolUtil.getWebRootPath("/");
                File tarDir = new File(webRootPath1, uploadDir);
                if (!tarDir.exists()) {
                    tarDir.mkdirs();
                }
                webRootPath = tarDir.getPath();
            }
            fileSave = new File(webRootPath, fileName);
            FileUtil.writeBytes(fileData, fileSave);
        }*/
        return fileName;
    }
}
